package com.tueye.web.controller.usermanager;


import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.tueye.entity.user.ParticularUserInfo;
import com.tueye.entity.user.User;
import com.tueye.service.usermanager.IRegisterService;
import com.tueye.utils.DateUtils;
import com.tueye.utils.TuEyeConstant;
import com.tueye.utils.UUIDGenerator;


@SuppressWarnings("unchecked")
@Controller
public class BaseUserinfoController {
	private Log log = LogFactory.getLog(BaseUserinfoController.class);
	
	@Autowired
	@Qualifier("registerService")
	private IRegisterService registerService;
 
 //查询用户基本信息
	@RequestMapping("/userinfoset.do") 
	public String infoset (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
        if(user  == null){
        	 return "pages/topics/usermanager/userlogin";  
        }
        request.getSession().setAttribute("user", user);
	//	request.setAttribute("user", user); 
        return "pages/topics/usermanager/userinfoset";   
		
	}
	//更新用户基本信息
	@RequestMapping("/updateUserinfo.do") 
	public String updateUserinfo (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String byname = (String)request.getParameter("byname");
		byname = java.net.URLDecoder.decode(byname , "UTF-8");//解码
		String nowHome = (String)request.getParameter("nowHome");
		nowHome = java.net.URLDecoder.decode(nowHome , "UTF-8");//解码
		String activeArea = (String)request.getParameter("activeArea");
		activeArea = java.net.URLDecoder.decode(activeArea , "UTF-8");//解码
		String homeplace = (String)request.getParameter("homeplace");
		homeplace = java.net.URLDecoder.decode(homeplace , "UTF-8");//解码
		String intoduce = (String)request.getParameter("intoduce");
		intoduce = java.net.URLDecoder.decode(intoduce , "UTF-8");//解码
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		List list=registerService.getUserBybyname(byname);
		System.out.println("$$$$$$$$$$$$$$$$$$$$"+list.size());
		if(list.size()==0){ 
	        user.setActiveArea(activeArea);
	        user.setByname(byname);
	        user.setHomeplace(homeplace);
	        user.setNowHome(nowHome);
	        user.setIntoduce(intoduce);
	        user.setSys_time(DateUtils.getCurrentDate());
	        request.getSession().setAttribute("user", user);
	        registerService.saveUser(user);
			response.getWriter().write("true"); 
			  }else{
					String byname2 = user.getByname();
					if(byname2.equals(byname)){
						
						user.setActiveArea(activeArea);
						user.setByname(byname);
				        user.setHomeplace(homeplace);
				        user.setNowHome(nowHome);
				        user.setIntoduce(intoduce);
				        user.setSys_time(DateUtils.getCurrentDate());
				       
				        //session.merge("user");
				        request.getSession().setAttribute("user", user);
				        registerService.updateUser(user);
						response.getWriter().write("true"); 
					}else{
						response.getWriter().write("false"); 
					}
					
			  }
		
		return null; 
		
	}
	//修改用户头像
	@RequestMapping("/updateUserface.do") 
	public String updateUserface (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String userPoto = (String)request.getParameter("userPoto");
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file  = multipartRequest.getFile("adpicture_site_local");
		String name = file.getOriginalFilename(); 
		if(name == null || "".equals(name)){
			user.setUserPoto(userPoto);
		}else{
			userPoto = null;
			String newpath = TuEyeConstant.PROJECT_TOPIC_PATH +File.separatorChar+"usermanager/userphoto"+File.separatorChar ; 
			file.transferTo(new File(newpath+name));
			userPoto = name;
			user.setUserPoto(userPoto);
		}
		registerService.saveUser(user);
		request.getSession().setAttribute("user", user);
		response.sendRedirect("userinfoset.do");
        return null;   
		
	}
	//查到详细信息
	@RequestMapping("/moreUserinfo.do") 
	public String moreUserinfo (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		if(user  == null){
       	 return "pages/topics/usermanager/userlogin";  
        }
		String id = user.getId();	
		List list = registerService.getParticularuserinfo(id);
		ParticularUserInfo pu;
		if(list.size()==0){
			pu = new ParticularUserInfo();
			pu.setId(UUIDGenerator.getUUID());
		}else{
			pu = (ParticularUserInfo) list.get(0);
		}
		request.setAttribute("pu", pu);
        return "pages/topics/usermanager/moreUserinfo";   
		
	}
	
//更新详细信息
	@RequestMapping("/updateMoreUserinfo.do") 
	public String updateMoreUserinfo (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String msn = (String)request.getParameter("msn");
		String qq = (String)request.getParameter("qq");
		String id = (String)request.getParameter("id");
		int sex = 0;
		int tixing = 0;
		int loveType = 0;
		if(request.getParameter("sex") != null && !"".equals(request.getParameter("sex"))) { 
			 sex = Integer.parseInt(request.getParameter("sex"));
		}
		if(request.getParameter("TiXing") != null && !"".equals(request.getParameter("TiXing"))) { 
		   tixing = Integer.parseInt(request.getParameter("TiXing"));
		}
		if(request.getParameter("loveType") != null && !"".equals(request.getParameter("loveType"))) { 
		   loveType = Integer.parseInt(request.getParameter("loveType"));
		}
		String birthday = (String)request.getParameter("birthday");
		String star_type = (String)request.getParameter("star_type");	
		String workType = (String)request.getParameter("workType");
		workType = java.net.URLDecoder.decode(workType , "UTF-8");//解码
		String over_school = (String)request.getParameter("over_school");
		over_school = java.net.URLDecoder.decode(over_school , "UTF-8");//解码
		String userWebSite = (String)request.getParameter("userWebSite");
		userWebSite = java.net.URLDecoder.decode(userWebSite , "UTF-8");//解码
		String loveThings = (String)request.getParameter("loveThings");
		loveThings = java.net.URLDecoder.decode(loveThings , "UTF-8");//解码
		String movierAuthor = (String)request.getParameter("movierAuthor");
		movierAuthor = java.net.URLDecoder.decode(movierAuthor , "UTF-8");//解码
		String music = (String)request.getParameter("music");
		music = java.net.URLDecoder.decode(music , "UTF-8");//解码
		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");         
		Date date = null;    
		try {    
			              date = format.parse(birthday);  // Wed sep 26 00:00:00 CST 2007    
			            } catch (ParseException e) {    
			                e.printStackTrace();    
			            }    

         
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		
		ParticularUserInfo pu = new ParticularUserInfo();
		pu.setId(id);
		pu.setBirthday(date);
		pu.setLoveType(loveType);
		pu.setMovierAuthor(movierAuthor);
		pu.setMsn(msn);
		pu.setMusic(music);
		pu.setOver_school(over_school);
		pu.setQq(qq);
		pu.setSex(sex);
		pu.setStar_type(star_type);
		pu.setSys_time(DateUtils.getCurrentDate());
		pu.setTiXing(tixing);
		pu.setUserWebSite(userWebSite);
		pu.setWorkType(workType);
		pu.setLoveThings(loveThings);
		pu.setUser(user);
        registerService.updateParticularUserInfo(pu);
				
		
		return null; 
		
	}
	
	@RequestMapping("/useraccount.do") 
	public String useraccount (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
        if(user  == null){
        	 return "pages/topics/usermanager/userlogin";  
        }
        request.getSession().setAttribute("user", user);
        return "pages/topics/usermanager/useraccount";   
		
	}
	
	@RequestMapping("/updateAccount.do") 
	public String updateAccount (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String password = (String)request.getParameter("password");
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		user.setPassword(password);
		registerService.saveUser(user);
		request.getSession().setAttribute("user", user);
        return null;   
		
	}
	

}
